package xml;

import xml.elements.DataItem;
import xml.elements.DataItemInput;
import xml.elements.DataItemSet;
import xml.elements.DataItemSetInput;
import xml.elements.EnumeratedUserInput;
import xml.elements.LogicalOperator;
import xml.elements.LogicalOperatorUserInput;
import xml.elements.OptionalDataItemInput;
import xml.elements.OptionalDataItemSetInput;
import xml.elements.OptionalEnumeratedUserInput;
import xml.elements.OptionalLogicalOperatorUserInput;
import xml.elements.OptionalStructureInput;
import xml.elements.OptionalStructureSetInput;
import xml.elements.State;
import xml.elements.Structure;
import xml.elements.StructureInput;
import xml.elements.StructureSet;
import xml.elements.StructureSetInput;
import xml.elements.SubState;
import xml.elements.Text;
import xml.others.FacadeConstructorUID;

public class FactoryState extends ScannerStateAbstract {

	String idState;
	String idUID;

	public FactoryState(String idUID, State stateSource) {
		this.idUID = idUID;
		this.idState = stateSource.getId();
		scanState(stateSource);
	}

	@Override
	protected void dataItemFound(DataItem o) {
		FacadeConstructorUID.getInstance().insertOutputDataItem(idUID, idState,
				o.getName(), o.getDomain());
	}

	@Override
	protected void dataItemInputFound(DataItemInput o) {

		FacadeConstructorUID.getInstance().insertInputDataItem(idUID, idState,
				o.getName(), o.getDomain());

	}

	@Override
	protected void enumeratedUserInputFound(EnumeratedUserInput o) {

		int max = -1;
		int min = 0;

		if (o.getMin() != null)
			min = o.getMin();
		if (o.getMax() != null)
			max = o.getMax();

		FacadeConstructorUID.getInstance().insertInputEnumeratedData(idUID,
				idState, o.getName(), o.getOptions(), min, max);

	}

	@Override
	protected void logicalOperatorUserInputFound(LogicalOperatorUserInput o) {

		String id1, name1, id2, name2, domain1, domain2;

		try {

//			String[] a = o.getDataItem1().replaceAll(" ", "").split(":");
			String[] a = o.getDataItem1().trim().split(":");
			id1 = a[0];
			name1 = a[1];
			domain1 = a[2];

		} catch (Exception e) {
			id1 = o.getDataItem1();
			name1 = o.getDataItem1();
			domain1 = null;
		}

		try {

//			String[] a = o.getDataItem2().replaceAll(" ", "").split(":");
			String[] a = o.getDataItem2().trim().split(":");
			id2 = a[0];
			name2 = a[1];
			domain2 = a[2];

		} catch (Exception e) {
			id2 = o.getDataItem2();
			name2 = o.getDataItem2();
			domain2 = null;
		}

		if (o.getOperator() == null
				|| o.getOperator().equals(LogicalOperator.XOR)) {
			FacadeConstructorUID.getInstance().insertInputXORDataEntry(idUID,
					idState, id1, name1, domain1, id2, name2, domain2);
		} else {
			FacadeConstructorUID.getInstance().insertInputORDataEntry(idUID,
					idState, id1, name1, domain1, id2, name2, domain2);
		}

	}

	@Override
	protected void optionalDataItemInputFound(OptionalDataItemInput o) {

		FacadeConstructorUID.getInstance().insertInputOptionalDataItem(idUID,
				idState, o.getName(), o.getDomain());

	}

	@Override
	protected void optionalEnumeratedUserInputFound(
			OptionalEnumeratedUserInput o) {

		FacadeConstructorUID.getInstance().insertOptionalInputEnumeratedData(
				idUID, idState, o.getName(), o.getOptions(), o.getMin(),
				o.getMax());

	}

	@Override
	// N�o existe na nova DTD. Adiciona um LogicalOperatorUserInput.
	protected void optionalLogicalOperatorUserInputFound(
			OptionalLogicalOperatorUserInput o) {

		String id1, name1, id2, name2, domain1, domain2;

		try {

//			String[] a = o.getDataItem1().replaceAll(" ", "").split(":");
			String[] a = o.getDataItem1().trim().split(":");
			id1 = a[0];
			name1 = a[1];
			domain1 = a[2];

		} catch (Exception e) {
			id1 = o.getDataItem1();
			name1 = o.getDataItem1();
			domain1 = null;
		}

		try {

//			String[] a = o.getDataItem2().replaceAll(" ", "").split(":");
			String[] a = o.getDataItem2().trim().split(":");
			id2 = a[0];
			name2 = a[1];
			domain2 = a[2];

		} catch (Exception e) {
			id2 = o.getDataItem2();
			name2 = o.getDataItem2();
			domain2 = null;
		}

		if (o.getOperator() == null
				|| o.getOperator().equals(LogicalOperator.XOR)) {
			FacadeConstructorUID.getInstance().insertInputXORDataEntry(idUID,
					idState, id1, name1, domain1, id2, name2, domain2);
		} else {
			FacadeConstructorUID.getInstance().insertInputORDataEntry(idUID,
					idState, id1, name1, domain1, id2, name2, domain2);
		}

	}

	@Override
	protected void optionalStructureInputFound(OptionalStructureInput o) {

		FacadeConstructorUID.getInstance().insertInputOptionalStructure(idUID,
				idState, o.getName(), o.getData());

	}

	@Override
	protected void structureFound(Structure o) {

		FacadeConstructorUID.getInstance().insertOutputStructure(idUID,
				idState, o.getName(), o.getData());

	}

	@Override
	protected void structureInputFound(StructureInput o) {

		FacadeConstructorUID.getInstance().insertInputStructure(idUID, idState,
				o.getName(), o.getData());

	}

	@Override
	protected void subStateFound(SubState o) {

		FacadeConstructorUID.getInstance().insertSubState(idUID, idState,
				o.getId());

		new FactorySubState(idUID, idState, o);

	}

	@Override
	protected void textFound(Text o) {

		FacadeConstructorUID.getInstance().insertText(idUID, idState,
				o.getId(), o.getValue());

	}

	@Override
	protected void dataItemSetFound(DataItemSet o) {

		int max = -1;
		int min = 0;

		if (o.getMin() != null)
			min = o.getMin();
		if (o.getMax() != null)
			max = o.getMax();

		FacadeConstructorUID.getInstance().insertOutputSetDataItem(idUID,
				idState, o.getName(), o.getDomain(), min, max);

	}

	@Override
	protected void dataItemSetInputFound(DataItemSetInput o) {

		int max = -1;
		int min = 0;

		if (o.getMin() != null)
			min = o.getMin();
		if (o.getMax() != null)
			max = o.getMax();

		FacadeConstructorUID.getInstance().insertInputSetDataItem(idUID,
				idState, o.getName(), o.getDomain(), min, max);

	}

	@Override
	protected void optionalDataItemSetInputFound(OptionalDataItemSetInput o) {

		int max = -1;
		int min = 0;

		if (o.getMin() != null)
			min = o.getMin();
		if (o.getMax() != null)
			max = o.getMax();

		FacadeConstructorUID.getInstance().insertInputOptionalSetDataItem(
				idUID, idState, o.getName(), o.getDomain(), min, max);

	}

	@Override
	protected void optionalStructureSetInputFound(OptionalStructureSetInput o) {

		int max = -1;
		int min = 0;

		if (o.getMin() != null)
			min = o.getMin();
		if (o.getMax() != null)
			max = o.getMax();

		FacadeConstructorUID.getInstance().insertInputOptionalSetStructure(
				idUID, idState, o.getName(), o.getData(), min, max);

	}

	@Override
	protected void structureSetFound(StructureSet o) {

		int max = -1;
		int min = 0;

		if (o.getMin() != null)
			min = o.getMin();
		if (o.getMax() != null)
			max = o.getMax();

		FacadeConstructorUID.getInstance().insertOutputSetStructure(idUID,
				idState, o.getName(), o.getData(), min, max);
	}

	@Override
	protected void structureSetInputFound(StructureSetInput o) {

		int max = -1;
		int min = 0;

		if (o.getMin() != null)
			min = o.getMin();
		if (o.getMax() != null)
			max = o.getMax();

		FacadeConstructorUID.getInstance().insertInputSetStructure(idUID,
				idState, o.getName(), o.getData(), min, max);

	}

}
